<template>
	<view class="uni-container flex flex-col h-full">
		<div>
			<u-button type="success" size="default" @click="scanCode">扫描二维码</u-button>
		</div>
		<div class="flex-auto overflow-auto">
			<u-form ref="uForm">
				<u-form-item label="分类">
					<u-input v-model="formData.typeName" @click="typeShow=true" disabled="isView" />
					<u-select v-model="typeShow" :list="typeList" @confirm="typeConfirm"></u-select>
				</u-form-item>
				<u-form-item label="库存">
					<u-input v-model="formData.stocks" disabled="isView" />
				</u-form-item>
				<u-form-item label="封面" v-if="formData.photoUrl">
					<u-image height="200rpx" mode="heightFix" :src="formData.photoUrl"></u-image>
				</u-form-item>
				<u-form-item label="书名">
					<u-input v-model="formData.name" />
				</u-form-item>
				<u-form-item label="作者">
					<u-input v-model="formData.author" />
				</u-form-item>
				<u-form-item label="价格">
					<u-input v-model="formData.price" />
				</u-form-item>
				<u-form-item label="简介">
					<u-input type="textarea" v-model="formData.description" />
				</u-form-item>
			</u-form>
			<!-- <uni-forms ref="form" :modelValue="formData" validate-trigger="submit" err-show-type="toast">
				<uni-forms-item name="name" label="书名">
					<u-input v-model="formData.name" />
				</uni-forms-item>
				<uni-forms-item name="isbn" label="">
					<uni-easyinput v-model="formData.isbn"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="author" label="">
					<uni-easyinput v-model="formData.author"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="press" label="">
					<uni-easyinput v-model="formData.press"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="info" label="">
					<uni-easyinput v-model="formData.info"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="description" label="">
					<u-input type="textarea" v-model="formData.description" />
					<!-- <uni-easyinput v-model="formData.description"></uni-easyinput>
				</uni-forms-item>
			</uni-forms> -->
		</div>
		<view class="uni-button-group">
			<button type="primary" class="uni-button" @click="submit">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/books.js';

	const db = uniCloud.database();
	const dbCollectionName = 'books';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
				result[key] = validator[key]
			}
		}
		return result
	}

	export default {
		data() {
			return {
				isView:true,
				typeShow: false,
				typeList: [{
						value: '1',
						label: '江'
					},
					{
						value: '2',
						label: '湖'
					}
				],
				formData: {
					stocks:1
				},
				formOptions: {}
			}
		},
		onReady() {
			// this.getBook()
			// this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 触发表单提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.submitForm()
				// this.$refs.form.validate().then((res) => {
				// 	this.submitForm(res)
				// }).catch(() => {
				// 	uni.hideLoading()
				// })
			},

			submitForm(value) {
				let sendData = {
					type: this.formData.type,
					stocks: this.formData.stocks,
					name: this.formData.name,
					author: this.formData.author,
					code: this.formData.code,
					douban: this.formData.douban,
					photoUrl: this.formData.photoUrl,
					price: this.formData.price,
					description: this.formData.description
				}
				// 使用 clientDB 提交数据
				db.collection(dbCollectionName).add(sendData).then((res) => {
					uni.showToast({
						icon: 'none',
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			},
			typeConfirm(e){
				this.formData.typeName = e[0].label
				this.formData.type = parseInt(e[0].value)
			},
			scanCode() {
				let _that = this;
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						_that.getBook(res.result)
					}
				});
			},
			getBook(id) {
				// https://api.zuk.pw/situ/book/isbn/9787516830475
				// {
				//   "ret": 0,
				//   "msg": "请求成功",
				//   "data": {
				//     "id": 9787119065892,
				//     "name": "心理学与藏心术",
				//     "subname": null,
				//     "author": "瀚林腾跃",
				//     "translator": null,
				//     "publishing": "外文",
				//     "published": "2010-10",
				//     "designed": "",
				//     "code": "9787119065892",
				//     "douban": 5297768,
				//     "doubanScore": 0,
				//     "brand": null,
				//     "weight": null,
				//     "size": null,
				//     "pages": "254",
				//     "photoUrl": "https://img1.doubanio.com/view/subject/m/public/s24936597.jpg",
				//     "localPhotoUrl": "",
				//     "price": "29.80元",
				//     "froms": "",
				//     "num": 0,
				//     "createTime": "2021-04-23T16:23:29",
				//     "uptime": "2021-08-11T16:27:21",
				//     "authorIntro": null,
				//     "description": "人心难窥测。于是始有读心术和藏心术。藏心术的关键在于正己保身，减少自我损伤。    藏心的最高境界是无形。无形是道，是世界万事万物的最高境界。懂得藏心，自然能够修炼读心术．从一言一行上挖掘出他人的蛛丝马迹。"
				//   }
				// }
				uni.showLoading({
					mask: true
				})
				uni.request({
					url: 'https://api.zuk.pw/situ/book/isbn/' + id, //仅为示例，并非真实接口地址。
					data: {
						text: 'uni.request'
					},
					header: {
						'custom-header': 'hello' //自定义请求头信息
					},
					success: (res) => {
						// this.formData = Object.assign(this.formData,res.data.data)
						this.formData={...this.formData,...res.data.data}
						console.log(1,this.formData);
						uni.hideLoading()
					}
				});
			}
		}
	}
</script>

<style>
	.uni-container {
		padding: 15px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;

	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
	}

	.uni-button {
		width: 184px;
		padding: 12px 20px;
		font-size: 14px;
		border-radius: 4px;
		line-height: 1;
		margin: 0;
	}
</style>
